package com.ruoyi.datastatistics.Agreement;

public class CharacterParsingCommon {
    public static void main(String[] args) {
        byte[] bytes = {(byte)0xFF, (byte)0xFE}; // 示例byte数组
        int decimal = byteArrayToDecimal(bytes);
        System.out.println("十进制数为: " + decimal);
    }
//byte数组转10进制数
    public static int byteArrayToDecimal(byte[] bytes) {
        int result = 0;
        for (byte b : bytes) {
            result = (result << 8) | (b & 0xFF); // 将byte数组每个字节转换为十进制并累加
        }
        return result;
    }
    //dongjian协议
    /**
     * 16tobcd 补零,高位在后,eg->0x0c 0x02:0c02
     * @param bit
     * @return
     */
    public static String get16tobcd(byte[] bit,boolean isHIB)
    {
        String str = "";
        for (int i = 0; i < bit.length; i++)
        {
            String temp = Integer.toHexString(bit[i]& 0xFF);
            temp = temp.length() < 2 ? ("0" + temp) : temp;
            str = isHIB? (temp + str):(str + temp);
        }
        return str;
    }


    /**
     * 16tobcd 补零,高位在后,eg->0x0c 0x02:0c02
     * @param str
     * @param isHIB
     * @return
     */
    public static byte[] get16tobcd(String str,boolean isHIB)
    {
        // 计算输入字符串的长度
        int length = str.length();
        // 检查字符串长度是否为偶数
        if (length % 2 != 0) {
            throw new IllegalArgumentException("The length of the hex string must be even.");
        }
        // 创建字节数组，长度为输入字符串长度的一半
        byte[] bit = new byte[length / 2];
        // 遍历字符串，将每两个字符转换为一个字节
        for (int i = 0; i < length / 2; i++) {
            String temp = str.substring(i * 2, i * 2 + 2);
            byte bcdByte = (byte) Integer.parseInt(temp, 16);
            // 根据isHIB参数决定字节存储的位置
            if (isHIB) {
                bit[i] = bcdByte;
            } else {
                bit[length / 2 - i - 1] = bcdByte;
            }
        }
        return bit;
    }

    /**
     * 东健 获取流量单位-统一M3
     * @param bit
     * @return
     */
    public static double getUnitM3(byte bit){
        double unit = 0.001;
        switch (bit){
            case (byte)0x2b:
                unit = 0.001;
                break;
            case (byte)0x2c:
                unit = 0.01;
                break;
            case (byte)0x2D:
                unit = 0.1;
                break;
            case (byte)0x2E:
                unit = 1;
                break;
            case (byte)0x2A:
                unit = 0.0001;
                break;
            case (byte)0x29:
                unit = 0.00001;
                break;
        }
        return unit;
    }
    /**
     * 东健 获取流速单位-统一M3/h
     * @param bit
     * @return
     */
    public static double getUnitM(byte bit){
        double unit = 0.001;
        switch (bit){
            case (byte)0x32:
                unit = 0.0000001;
                break;
            case (byte)0x33:
                unit = 0.000001;
                break;
            case (byte)0x34:
                unit = 0.00001;
                break;
            case (byte)0x35:
                unit = 0.0001;
                break;
            case (byte)0x36:
                unit = 0.001;
                break;
            case (byte)0x37:
                unit = 0.01;
                break;
        }
        return unit;
    }
    /**
     * 验证和
     * @param bit
     * @return
     */
    public static byte getTotalForByte(byte[] bit)
    {
        int sum = 0;
        if (bit.length > 0)
        {
            for (int i = 0; i < bit.length; i++)
            {
                sum = sum + bit[i];
                sum = sum & 0xFF;
            }
        }
        byte rein = (byte)sum;
        return rein;
    }
    public static String convertString(byte[] data){
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<data.length; i++){
            String s = Integer.toHexString(data[i] & 0xff);
            s = s.length()<2 ?("0"+s):s;
            sb.append(s);
        }
        return sb.toString();
    }
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    /**
     * 转化字符串为十六进制编码
     */
    public static String toHexString(String s){
        String str="";
        for (int i=0;i<s.length();i++){
            int ch = (int)s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }
        return str;
    }

    /**
     * bcd码,示例：109230700049-》0x10 0x92 0x30 0x70 ·······
     * @param ad
     * @return
     */
    public static byte[] getByte2(String ad)
    {
        int length = ad.length();
        byte[] bit = new byte[length/2];

        for (int i = 0; i < length/2; i++)
        {
            String temp = ad.substring(i * 2, i * 2+2);
            bit[i] = (byte)Integer.parseInt(temp,16);
        }
        return bit;
    }

}
